package com.edol.painting.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.signature.StringSignature;
import com.edol.painting.utils.Constant;
import com.doodle.common.utils.Utils;
import com.edol.painting.R;
import com.edol.painting.data.PaintingDatabase;

import java.util.List;

/**
 * Created by Administrator on 2017/7/3.
 */

public class PaintListAdapter extends BaseAdapter {
    private List<PaintingDatabase.PaintingEntry> mData;
    private Context mContext;

    private int mMaxThumbSize;

    public PaintListAdapter(Context context) {
        mContext = context;
        init(context);
    }

    public PaintListAdapter(Context context, List<PaintingDatabase.PaintingEntry> data) {
        this(context);
        mData = data;
    }

    private void init (Context context) {
        //column: 2, padding 5dp
        int column = Constant.PAINTING_LIST_NUM_COLUMNS;
        int padding = 5; //dp
        int w = context.getResources().getDisplayMetrics().widthPixels;
        mMaxThumbSize = (w - Utils.dipToPixel((column + 1) * padding)) / 2;
    }

    public void setData(List<PaintingDatabase.PaintingEntry> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public List<PaintingDatabase.PaintingEntry> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mData != null ? mData.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = initHolder();
        }

        holder = (Holder) convertView.getTag();
        bindData(holder, mData.get(position));

        return convertView;
    }

    private View initHolder() {
        Holder holder = new Holder();
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_paint_list_item, null);
        holder.thumbView = (ImageView) view.findViewById(R.id.thumb);
        view.setTag(holder);
        return view;
    }

    private void bindData(Holder holder, PaintingDatabase.PaintingEntry entry) {
        Glide.with(mContext)
                .load(entry.mThumbnail)
                .animate(R.anim.img_load_fade)
                .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                .into(getImgViewTarget(holder.thumbView));
    }

    private static class Holder {
        private ImageView thumbView;
    }

    protected GlideDrawableImageViewTarget getImgViewTarget(final ImageView imgView) {
        return new GlideDrawableImageViewTarget(imgView) {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                super.onResourceReady(resource, animation);
                if (resource instanceof GlideBitmapDrawable) {
                    Bitmap bmp = ((GlideBitmapDrawable) resource).getBitmap();
                    if (bmp != null) {
                        ViewGroup.LayoutParams params = imgView.getLayoutParams();
                        //depend width
                        int w = mMaxThumbSize;
                        int h = (int) ((bmp.getHeight() / (float) bmp.getWidth()) * mMaxThumbSize);
                        params.width = w;
                        params.height = h;
                    }

                    imgView.setImageBitmap(bmp);
                }
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);

            }
        };
    }
}
